﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MMPS.Entity.POCOMdel;
using MMPS.Entity.ViewModel;
using MMPS.Common;

namespace MMPS.Business
{
   public  class MemCardsService:BaseService<MemCards>
    {
        //返回类型都是公共类型,添加引用common
        /// <summary>
        /// 查询列表信息
        /// </summary>
        /// <param name="viewModel"></param>
        /// <returns></returns>
        #region   查询
        public OperateResult MerCardInfo(MemCardsPageViewModel viewModel)
        {
            //先查询，默认等于true
            var query = PredicateExtensions.True<MemCards>();
            //判断是否有条件，有条件加条件，没条件默认加载
            //if (!string.IsNullOrWhiteSpace(viewModel.MC_CardID))
            //{
            //    query = query.And(m=>m.MC_CardID.Contains(viewModel.MC_CardID));
            //}
            //if (!string.IsNullOrWhiteSpace(viewModel.MC_Name))
            //{
            //    query = query.And(m=>m.MC_Name.Contains(viewModel.MC_Name));
            //}
            //if (!string.IsNullOrWhiteSpace(viewModel.MC_Mobile))
            //{
            //    query = query.And(m => m.MC_Mobile.Contains(viewModel.MC_Mobile));
            //}

            //if (!string.IsNullOrWhiteSpace(viewModel.MC_State.ToString()))
            //{
            //    query = query.And(m => m.MC_State==viewModel.MC_State);
            //}

            //if (!string.IsNullOrWhiteSpace(viewModel.CL_LevelName))
            //{
            //    query = query.And(m => m.CardLevels.CL_LevelName.Contains(viewModel.CL_LevelName));
            //}
            //定义总条数的初始值
            int rowsCount = 0;
            //拼接
            var list = GetList(viewModel.page, viewModel.rows, ref rowsCount, query, m => m.MC_ID, true).Select(m => new MemCardsListViewModel
            {
                MC_CardID = m.MC_CardID,
                MC_ID=m.MC_ID,
                MC_CreateTime = m.MC_CreateTime,
                MC_Mobile = m.MC_Mobile,
                MC_Name = m.MC_Name,
                MC_Point = m.MC_Point,
                MC_Sex = m.MC_Sex,
                MC_State = m.MC_State,
                MC_TotalMoney = m.MC_TotalMoney,
                CL_LevelName = m.CardLevels.CL_LevelName,

            }).ToList();
            //获取返回数据
            var dataGrid = new DatgRIDViewModel
            {
                total = rowsCount,
                rows = list,
            };
            return new OperateResult(ResultStatus.Success, "", dataGrid);

        }
        #endregion



        #region   添加
        public OperateResult MemCardsCreateInfo(MemCardsListViewModel viewModel)
        {
            //返回公共类型
            OperateResult result = null;
            //判断该用户是否注册过
            var model = Find(m => m.MC_Name == viewModel.MC_Name);
            if (model!=null)
            {
                result =new OperateResult(ResultStatus.Error, "该用户注册过");
            }
            else
            {
                //把MemCardsListViewModel类型的model转换成MemCards类型的model
                MemCards mem = new MemCards();
                mem.MC_CardID = viewModel.MC_CardID;
                mem.MC_Name = viewModel.MC_Name;
                mem.MC_Mobile = viewModel.MC_Mobile;
                mem.MC_Password = viewModel.MC_Password;
                mem.MC_Sex = viewModel.MC_Sex;
                mem.CL_ID = viewModel.CL_ID;
                mem.MC_RefererCard = viewModel.MC_RefererCard;
                mem.MC_RefererName = viewModel.MC_RefererName;
                //执行添加，添加成功返回公共类型
                if (Add(mem))
                {
                    result = new OperateResult(ResultStatus.Success, "添加成功！");
                }
                else
                {
                    result = new OperateResult(ResultStatus.Error, "添加失败！");
                }
            }
            return result;
        }
        #endregion


        #region   根据id查询对应信息
        #endregion


        #region   修改
        #endregion

        #region   删除
        #endregion
    }
}
